﻿using System.Diagnostics;
using System.Security.Principal;
using System.Windows;

namespace XYAC.Framework.MonitorTireMaterialOnline.Extensions
{
    public static class AdminHelper
    {
        public static void EnsureAdmin()
        {
            // Check if the application is not running as administrator
            if (!IsAdministrator())
            {


                try
                {
                    RestartApplication();

                }
                catch (Exception ex)
                {

                }
            }
        }
        private static void RestartApplication()
        {
            // 获取当前应用程序的启动路径
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = "DelayStart.vbs", // 或者是 "Delay.bat"
                UseShellExecute = true
            };
            //灾难性故障时重新启动
            Process.Start(startInfo);
        }

        private static bool IsAdministrator()
        {
            var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }
}
